package com.ruoyi.mp.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mp.domain.MpUserTemplateinfo;
import com.ruoyi.mp.service.IMpAccountService;
import com.ruoyi.mp.service.IMpUserTemplateinfoService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustryEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/templateinfo")
public class MpTemplateController  extends BaseController {

    @Autowired
    private IMpAccountService iMpAccountService;
    @Autowired
    private IMpUserTemplateinfoService mpUserTemplateInfoService;
    /**
     * 查询模版管理列表
     */
    @RequiresPermissions("mp:templateInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(MpUserTemplateinfo mpUserTemplateinfo)
    {
        startPage();
        List<MpUserTemplateinfo> list = mpUserTemplateInfoService.selectMpUserTemplateinfoList(mpUserTemplateinfo);
        return getDataTable(list);
    }

    /**
     * 导出模版管理列表
     */
    @RequiresPermissions("mp:templateInfo:export")
    @Log(title = "模版管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MpUserTemplateinfo mpUserTemplateinfo)
    {
        List<MpUserTemplateinfo> list = mpUserTemplateInfoService.selectMpUserTemplateinfoList(mpUserTemplateinfo);
        ExcelUtil<MpUserTemplateinfo> util = new ExcelUtil<MpUserTemplateinfo>(MpUserTemplateinfo.class);
        util.exportExcel(response, list, "模版管理数据");
    }

    /**
     * 获取模版管理详细信息
     */
    @RequiresPermissions("mp:templateInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mpUserTemplateInfoService.selectMpUserTemplateinfoById(id));
    }

    /**
     * 新增模版管理
     */
    @RequiresPermissions("mp:templateInfo:add")
    @Log(title = "模版管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MpUserTemplateinfo mpUserTemplateinfo)
    {
        return toAjax(mpUserTemplateInfoService.insertMpUserTemplateinfo(mpUserTemplateinfo));
    }

    /**
     * 修改模版管理
     */
    @RequiresPermissions("mp:templateInfo:edit")
    @Log(title = "模版管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MpUserTemplateinfo mpUserTemplateinfo)
    {
        return toAjax(mpUserTemplateInfoService.updateMpUserTemplateinfo(mpUserTemplateinfo));
    }

    /**
     * 删除模版管理
     */
    @RequiresPermissions("mp:templateInfo:remove")
    @Log(title = "删除模版", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids,Long accountId) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(accountId).getAppId();
        return AjaxResult.success(mpUserTemplateInfoService.delPrivateTemplate(appId,ids));
    }

    /**
     * 设置行业信息
     */
    @PostMapping("/setIndustry/{id}")
    @RequiresPermissions("mp:template:setIndustry")
    @Log(title = "设置行业信息", businessType = BusinessType.OTHER)
    public AjaxResult 	setIndustry(@PathVariable Long id,int first,int secondary) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(mpUserTemplateInfoService.setIndustry(appId,first,secondary));
    }
    /**
     * 更新设置的行业信息
     */
    @GetMapping("/getIndustry/{id}")
    @RequiresPermissions("mp:template:getIndustry")
    @Log(title = "更新设置的行业信息", businessType = BusinessType.OTHER)
    public AjaxResult getIndustry(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(mpUserTemplateInfoService.getIndustry(appId));
    }

    /**
     * 更新所有模版
     */
    @PostMapping("/getAllPrivateTemplate/{id}")
    @RequiresPermissions("mp:template:getAllPrivateTemplate")
    @Log(title = "更新所有已添加模版", businessType = BusinessType.INSERT)
    public AjaxResult getAllPrivateTemplate(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(mpUserTemplateInfoService.getAllPrivateTemplate(appId));
    }



    @GetMapping("/getIndustryEnum")
    @Log(title = "获取枚举类", businessType = BusinessType.OTHER)
    public AjaxResult getIndustryEnum(){
        List<WxMpTemplateIndustryEnum> industries = Arrays.asList(WxMpTemplateIndustryEnum.values());
        return  AjaxResult.success(industries.stream()
                .map(industry -> {
                    Map<String, Object> industryInfo = new HashMap<>();
                    industryInfo.put("name", industry.name()); // 添加枚举名称
                    industryInfo.put("primaryIndustry", industry.getFirstClass());
                    industryInfo.put("deputyIndustry", industry.getSecondClass());
                    industryInfo.put("code", industry.getCode());
                    return industryInfo;
                })
                .collect(Collectors.toList()));
    }


    @GetMapping("/addTemplate/{id}")
    @RequiresPermissions("mp:template:addTemplate")
    @Log(title = "获取模版id", businessType = BusinessType.INSERT)
    public AjaxResult addTemplate(@PathVariable Long id,String shortTemplateId, List<String> keywordNameList) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(mpUserTemplateInfoService.addTemplate(appId,shortTemplateId,keywordNameList));
    }
}
